package com.xhj.xmly.activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.xhj.xmly.R;
import com.xhj.xmly.fragment.DiscoverFragment;
import com.xhj.xmly.fragment.DownLoadFragment;
import com.xhj.xmly.fragment.LoginFragment;
import com.xhj.xmly.fragment.MineFragment;
import com.xhj.xmly.fragment.SubscriptionFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private DiscoverFragment discoverFragment;
    private DownLoadFragment downLoadFragment;
    private MineFragment mineFragment;
    private SubscriptionFragment subscriptionFragment;
    private List<Fragment> fragmentList;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private DrawerLayout drawerLayout;
    private FrameLayout fram;
    private DrawerLayout drawerLayout_main;
    private FrameLayout fram_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawlayout);
        drawerLayout_main = ((DrawerLayout) findViewById(R.id.drawlayout_main));
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        drawerLayout_main.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        fram = (FrameLayout) findViewById(R.id.drawer_container);
        fram_main = ((FrameLayout) findViewById(R.id.drawer_container_main));
        LoginFragment loginFragment = new LoginFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.drawer_container_main,loginFragment).commit();
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                //这样，点击时 ，main界面不受影响
                drawerView.setClickable(true);
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            }

            //抽屉关闭状态下，不可滑出
            @Override
            public void onDrawerClosed(View drawerView) {
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
        drawerLayout_main.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                drawerView.setClickable(true);
                drawerLayout_main.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                drawerLayout_main.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
        initData();

    }


    private void initData() {
        discoverFragment = new DiscoverFragment();
        downLoadFragment = new DownLoadFragment();
        mineFragment = new MineFragment();
        subscriptionFragment = new SubscriptionFragment();

        fragmentList = new ArrayList<>();
        fragmentList.add(discoverFragment);
        fragmentList.add(subscriptionFragment);
        fragmentList.add(downLoadFragment);
        fragmentList.add(mineFragment);

        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            transaction.add(R.id.fragment_container, fragmentList.get(i));
            transaction.hide(fragmentList.get(i));
        }
        transaction.show(fragmentList.get(0));
        transaction.commit();


    }

    public void onClick(View view) {
        int position = 0;
        transaction = manager.beginTransaction();
        switch (view.getId()) {
            case R.id.btn_1:
                position = 0;
                break;
            case R.id.btn_2:
                position = 1;
                break;
            case R.id.btn_4:
                position = 2;
                break;
            case R.id.btn_5:
                position = 3;
                break;

        }
        for (int i = 0; i < fragmentList.size(); i++) {
            transaction.hide(fragmentList.get(i));
        }
        transaction.show(fragmentList.get(position));
        transaction.commit();
    }

    public void onClickImage(View view){
        Intent intent = new Intent(this, PlayActivity.class);
        startActivity(intent);
    }

//    @Override
//    public void onBackPressed() {
//        if (drawerLayout.isDrawerOpen(fram)|| drawerLayout_main.isDrawerOpen(fram_main)) {
//
//            drawerLayout.closeDrawers();
//            drawerLayout_main.closeDrawers();
//        } else {
//            super.onBackPressed();
//
//        }
//    }

    public DrawerLayout getDrawerLayout() {
        return drawerLayout;
    }

    public DrawerLayout getDrawerLayout_main() {
        return drawerLayout_main;
    }

    public FrameLayout getFram() {
        return fram;
    }

    public FrameLayout getFram_main() {
        return fram_main;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (drawerLayout.isDrawerOpen(fram) || drawerLayout_main.isDrawerOpen(fram_main)) {

                drawerLayout.closeDrawers();
                drawerLayout_main.closeDrawers();
                return true;
            } else {
                Dialog dialog = new AlertDialog.Builder(this)
                        .setTitle("温馨提示")
                        .setMessage("你确认退出喜马拉雅FM？")
                        .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).setNegativeButton("最小化", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                moveTaskToBack(isFinishing());
                            }
                        }).setNeutralButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                MainActivity.this.finish();
                            }
                        }).create();
                dialog.show();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
